﻿<div>
    <button onclick="recordingModule.startRecording()" id="record-button">Record</button>
    <button onclick="recordingModule.stopRecording()" id="stop-button" disabled>Stop</button>
    <button onclick="recordingModule.uploadRecording()" id="upload-button" disabled>Upload</button>
</div>

<script>
    var recordingModule = function () {

        var recorder, input, audio_context,

        supportsUserMedia = false,

        isUserMediaInited = false,

        initComplete = function () {
            recorder.clear();

            input.connect(audio_context.destination);
            recorder = new Recorder(input);

            recorder && recorder.record();
        },

        initUserMedia = function (onInited) {
            if (isUserMediaInited) {
                if (onInited) onInited();
            } else {
                try {
                    // webkit shim
                    window.AudioContext = window.AudioContext || window.webkitAudioContext;
                    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
                    window.URL = window.URL || window.webkitURL;

                    audio_context = new AudioContext;
                } catch (e) {
                    alert('No web audio support in this browser!');
                }

                navigator.getUserMedia({ audio: true }, function (stream) {
                    input = audio_context.createMediaStreamSource(stream);

                    input.connect(audio_context.destination);

                    recorder = new Recorder(input);

                    isUserMediaInited = true;

                    if (onInited) onInited();
                }, function (e) {
                    showAlert({ title: "Browser Issue", message: "The browser is not currently able to record audio." });
                    console.log('No live audio input: ' + e);
                });
            }
        };

        return {

            startRecording: function () {
                $("#record-button").prop('disabled', true);
                $("#stop-button").prop('disabled', false);
                $("#upload-button").prop('disabled', false);

                initUserMedia(initComplete);
            },

            stopRecording: function () {
                $("#record-button").prop('disabled', false);
                $("#stop-button").prop('disabled', true);
                $("#upload-button").prop('disabled', false);

                recorder && recorder.stop();
                input.disconnect();
            },

            uploadRecording: function () {
                $("#record-button").prop('disabled', true);
                $("#stop-button").prop('disabled', true);
                $("#upload-button").prop('disabled', true);

                recorder && recorder.exportWAV(function (blob) {
                    var form = new FormData();
                    form.append("file", blob);

                    $.ajax({
                        type: "POST",
                        url: "/upload/recording",
                        data: form,
                        processData: false,
                        contentType: false,
                    }).success(function (e) {
                        //todo
                        alert("upload complete");
                    }).error(function (e) {
                        alert("upload failed: " + e.message);
                    }).complete(function (e) {
                        $("#record-button").prop('disabled', false);
                        $("#stop-button").prop('disabled', true);
                        $("#upload-button").prop('disabled', true);
                    });
                });
            },

        }
    }();
</script>

